﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;
namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyHocPhan_ChiTiet : QldtSdh.GUI.Common.BaseDialog
    {
        HocPhan hocPhan;
        DonVi donVi;
        int countObjectAdded = 0;

        public event Common.TListChangedEventHandler OnChanged;

        public FormQuanLyHocPhan_ChiTiet(DonVi donVi, HocPhan hocPhan = null)
        {
            InitializeComponent();
            this.donVi = donVi;
            this.hocPhan = hocPhan;
            cmbHinhThucHoc.DataSource = BizHinhThucHoc.GetList();
            cmbHinhThucThi.DataSource = BizHinhThucThi.GetList();
            LoadDonVi();
            InitInterface();
        }

        void InitInterface()
        {
            if (hocPhan != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.HOCPHAN);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtMa.Text = hocPhan.MaHocPhan;
                txtTen.Text = hocPhan.TenHocPhan;
                cmbHinhThucHoc.Text = hocPhan.HinhThucHoc;
                cmbHinhThucThi.Text = hocPhan.HinhThucThi;
                cmbDonViPhuTrach.SelectedValue = hocPhan.MaDonViPhuTrachGUID;
                nDiemQTHT.Value = hocPhan.HeSoDiemQTHT;
                txtDiemThi.Text = (100 - nDiemQTHT.Value).ToString();
                nSTC.Value = hocPhan.SoTinChi;
                txtGhiChu.Text = hocPhan.GhiChu;
                txtTongSoGio.Text = (hocPhan.SoTinChi * 15).ToString();
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.HOCPHAN);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtMa.Text = "";
                txtTen.Text = "";
                cmbHinhThucHoc.SelectedIndex = 0;
                cmbHinhThucThi.SelectedIndex = 0;
                cmbDonViPhuTrach.SelectedValue = donVi.MaDonViGUID;
                nDiemQTHT.Value = 30;
                txtDiemThi.Text = "70";
                txtTongSoGio.Text = "30";
                nSTC.Value = 2;
                txtGhiChu.Text = "";
            }
            txtMa.Focus();
        }

        void LoadDonVi()
        {
            cmbDonViPhuTrach.DataSource = BizDonViDaoTao.GetList();
            cmbDonViPhuTrach.DisplayMember = "TenDonVi";
            cmbDonViPhuTrach.ValueMember = "MaDonViGUID";
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (txtMa.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtMa, string.Format(ERROR_CONSTANT_STRING.LOI_MARONG, ENTITY_NAME.HOCPHAN));
                txtMa.Focus();
                return false;
            }
            if (txtTen.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.HOCPHAN));
                txtTen.Focus();
                return false;
            }
            if (cmbHinhThucHoc.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbHinhThucHoc, string.Format(ERROR_CONSTANT_STRING.LOI_NOTYET_SELECT_ITEM, "Hình thức học"));
                cmbHinhThucHoc.Focus();
                return false;
            }
            if (cmbHinhThucThi.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbHinhThucThi, string.Format(ERROR_CONSTANT_STRING.LOI_NOTYET_SELECT_ITEM, "Hình thức thi"));
                cmbHinhThucThi.Focus();
                return false;
            }
            if (cmbDonViPhuTrach.SelectedIndex == -1)
            {
                errorNotify.SetError(cmbDonViPhuTrach, string.Format(ERROR_CONSTANT_STRING.LOI_DANHSACHRONG, ENTITY_NAME.DONVIDAOTAO));
                btnBoQua.Focus();
                return false;
            }
            return true;
        }

        private void nDiemQTHT_ValueChanged(object sender, EventArgs e)
        {
            int i = 0;
            if (int.TryParse(nDiemQTHT.Value.ToString(), out i))
                txtDiemThi.Text = (100 - int.Parse(nDiemQTHT.Value.ToString())).ToString();
            else
            {
                DialogUtils.ShowError("Nhập sai điểm quá trình học tập");
            }
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (hocPhan != null)
                {
                    #region Cập nhật thông tin
                    hocPhan.MaHocPhan = txtMa.Text;
                    hocPhan.TenHocPhan = txtTen.Text;
                    hocPhan.HinhThucHoc = cmbHinhThucHoc.Text;
                    hocPhan.HinhThucThi = cmbHinhThucThi.Text;
                    hocPhan.MaDonViPhuTrachGUID = cmbDonViPhuTrach.SelectedValue.ToString();
                    hocPhan.HeSoDiemQTHT = (int)nDiemQTHT.Value;
                    hocPhan.SoTinChi = (int)nSTC.Value;
                    hocPhan.GhiChu = txtGhiChu.Text;
                    hocPhan.TongSoGio = int.Parse(txtTongSoGio.Text);
                    var rs = BizHocPhan.Update(hocPhan);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtMa, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.HOCPHAN));
                        txtMa.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.HOCPHAN));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.HOCPHAN));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới
                    hocPhan = new HocPhan();
                    hocPhan.MaHocPhanGUID = System.Guid.NewGuid().ToString();
                    hocPhan.MaHocPhan = txtMa.Text;
                    hocPhan.TenHocPhan = txtTen.Text;
                    hocPhan.HinhThucHoc = cmbHinhThucHoc.Text;
                    hocPhan.HinhThucThi = cmbHinhThucThi.Text;
                    hocPhan.MaDonViPhuTrachGUID = cmbDonViPhuTrach.SelectedValue.ToString();
                    hocPhan.HeSoDiemQTHT = (int)nDiemQTHT.Value;
                    hocPhan.SoTinChi = (int)nSTC.Value;
                    hocPhan.GhiChu = txtGhiChu.Text;
                    hocPhan.TongSoGio = int.Parse(txtTongSoGio.Text);
                    //hocPhan.LoaiHocPhan = "";
                    var rs = BizHocPhan.Add(hocPhan);
                    hocPhan = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        countObjectAdded++;
                        OnChanged(this, new Common.EntityArgs(hocPhan));
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtMa, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.HOCPHAN));
                        txtMa.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.HOCPHAN));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.HOCPHAN));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyHocPhan_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (countObjectAdded > 0)
                DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void nSTC_ValueChanged(object sender, EventArgs e)
        {
            txtTongSoGio.Text = (nSTC.Value * 15).ToString();
        }

        private void lnkQuanLyDonVi_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var frm = new FormQuanLyDonVi();
            frm.ShowDialog();
            LoadDonVi();
        }
    }
}
